package com.course.rabbitmq.consumer;

import com.common.handler.ExchangeServiceHandler;
import com.course.service.ICourseService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 课程服务的MQ异步消息-消费者
 */
@Component
@Slf4j
public class CourseConsumer {

    @Resource
    ICourseService iCourseService;

    /**
     * 订单服务发送 -》课程销量 + 1
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "course-sales-increment-queue", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")),
            exchange = @Exchange(value = ExchangeServiceHandler.COURSE_EXCHANGE),
            key = "course.sales.increment"
    ))
    public void courseSaleIncrement(Long courseId) {
        log.info("课程服务接收消息：课程销量 + 1 {}", courseId);
        // 课程服务处理逻辑
        iCourseService.saleIncrement(courseId);
    }

}
